#include <iostream>
#include <string>
#include <unordered_map>
#include <cmath>
#include <ctime>
#include "Funtion.hpp"
#define EPOLL_WAIT_NUM EPOLL_WAIT_MAX
class EpollServe
{
public:
    EpollServe()
    {
        _sock = _serve.Creat();
        _fun.SetPort(_sock);
        _fun.Setftl(_sock);
        _serve.Bind();
        _serve.Listen();
        _epoll.Creat();
        Data *buffer = new Data;
        buffer->_serve = &_serve;
        buffer->_epoll = &_epoll;
        _shine.emplace(std::make_pair(_sock, buffer));
        _epoll.Ctl(EPOLL_CTL_ADD, _sock, EPOLLIN | EPOLLET);
    }
    void Start()
    {
        while(true)
        {
            int num = _epoll.Wait(_arr);
            switch (num)
            {
                case 0:
                    std::cout << "timeout..." << std::endl;
                    break;
                case -1:
                    std::cout << "epoll_wait fail..." << std::endl;
                    break;
                default:
                    _fun.handevent(num, _shine, _arr, _sock);
                    break;
            }
        }
    }

private:
    Func _fun;
    epoll_event _arr[EPOLL_WAIT_NUM];
    Epoll _epoll;
    Sock _serve;
    int _sock;
    std::unordered_map<int, Data *> _shine;
};